<?php
include ("./includes/connection.php");
$auxLat=(double)$_POST['nom'];

$auxLon=(double)$_POST['nom1'];


$sql = "Select Xi,Yi from recorregut join lloc on recorregut.idLlocInicial=lloc.idLloc";
$result = mysql_query($sql, $conexion);
while($row = mysql_fetch_array($result)) {

	$lat[]=$row['Xi'];
	$lon[]=$row['Yi'];

}

for ($i=0; $i <count($lat) ; $i++) {
	 
$result=distance((double)$lat[$i],(double)$lon[$i],$auxLat,$auxLon);

	if($result<7){
		$row1[]=array('lat' => $lat[$i],'lon'=> $lon[$i]);
	
	}
	
}

echo json_encode($row1);

function distance($lat1, $lon1, $lat2, $lon2) {
     $theta = $lon1 - $lon2;
    $dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) + cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta));
    $dist1 = acos($dist);
    $dist2 = rad2deg($dist1);
    $miles = (double)($dist2 * 60 * 1.1515* 1.609344);
    
        return $miles ;
   
}



?>